Mehr ggplot2!

library(tidyverse)

gapminder_dat_full <- readRDS(here::here("data", "gapminder_dat.rds"))

gapminder_dat_trend <- gapminder_dat_full %>%
  filter(time > 1990, time < 2022, country %in% c("nga", "zaf", "deu", "rus", "chn", "ind", "bra", "usa", "egy", "aus", "mex", "jpn"))

Facetting

ggplot(
  data = gapminder_dat_trend,
  mapping = aes(
    x = time,
    y = co2_pcap_cons,
    color = country
  )
) +
  geom_line(linewidth = 1.4) +
  theme_bg()

Lösung: Faceting

Faceting

Anordnen von einer einzelnen Variable in einem Raster:

facet_wrap()

ggplot(
  data = gapminder_dat_trend,
  mapping = aes(
    x = time,
    y = co2_pcap_cons,
    color = country
  )
) +
  geom_line(linewidth = 1.4) +
  facet_wrap(vars(country), nrow = 4) +
  theme_bg()

facet_grid

ggplot(
  data = gapminder_dat_trend,
  mapping = aes(
    x = time,
    y = co2_pcap_cons,
    color = country
  )
) +
  geom_line(linewidth = 1.4) +
  facet_grid(country ~ .) +
  theme_bg()

Facetting - Mehrere Variablen

Anordnen von mehreren Variable in einem Raster:

facet_wrap()

ggplot(
  data = gapminder_dat_trend,
  mapping = aes(
    x = time,
    y = co2_pcap_cons,
    color = country
  )
) +
  geom_point() +
  geom_line() +
  facet_wrap(vars(country, world_4region), nrow = 4) +
  theme_bg()

facet_grid()

ggplot(
  data = gapminder_dat_trend,
  mapping = aes(
    x = time,
    y = co2_pcap_cons,
    color = country
  )
) +
  geom_line(linewidth = 1.4) +
  facet_grid(country ~ world_4region) +
  theme_bg()

Facetting - Tipps

Plot alle Punkte

bg <- gapminder_dat_trend %>%
  mutate(country_bg = country) %>%
  select(-country)

ggplot(gapminder_dat_trend, aes(x = time, y = co2_pcap_cons, color = country, group = country)) +
  # background lines: drawn in every facet, grouped by country_bg
  geom_line(
    data = bg,
    aes(x = time, y = co2_pcap_cons, group = country_bg),
    inherit.aes = FALSE,
    color = "grey70",
    alpha = 0.5,
    linewidth = 0.4
  ) +
  # foreground points/lines for the focal country in each facet
  geom_line(linewidth = 1.5) +
  facet_wrap(vars(country)) +
  guides(color = "none") +
  theme_bg()

Facetting - Tipps

Plot Mittelwerte

ggplot(
  data = gapminder_dat_trend,
  mapping = aes(
    x = time,
    y = co2_pcap_cons,
    color = country
  )
) +
  geom_line(linewidth = 1) +
  geom_smooth(aes(group = income_groups), color = "grey") +
  facet_wrap(vars(income_groups)) +
  theme_bg()

Sortieren

Sortieren, läuft in ggplot2 generell über factor():

gapminder_dat_trend$income_groups_fac <- factor(gapminder_dat_trend$income_groups,
  levels = c("lower_middle_income", "upper_middle_income", "high_income")
)
ggplot(
  data = gapminder_dat_trend,
  mapping = aes(
    x = time,
    y = co2_pcap_cons,
    color = country
  )
) +
  geom_line(linewidth = 1) +
  geom_smooth(aes(group = income_groups), color = "grey") +
  facet_wrap(vars(income_groups_fac)) +
  theme_bg()

Skalen und Legenden

Skalen

“Scales in ggplot2 control the mapping from data to aesthetics. They take your data and turn it into something that you can see, like size, colour, position or shape.” ggplot2: Elegant Graphics for Data Analysis

Link to aes slide.

Legenden

Legenden werden automatisch erzeugt. Dafür werden die aestetics genutzt, also das mapping von Daten zu grafischen Elementen. Jede Skala bekommt eine Legende zugeordnet.

Legenden und Achsen sind funktional äquivalent und werden in ggplot2 unter dem Begriff guides zusammengefasst. Während Skalen die Daten auf grafische Eigenschaften wie Position oder Farbe abbilden, machen Guides diese Abbildung wieder verständlich: Achsen übersetzen Positionen zurück in Zahlen, Legenden ordnen Farben oder Symbole den entsprechenden Datenwerten zu. Man kann sie daher als die „Umkehrfunktion“ der jeweiligen Scales verstehen.

Jede aesthetic im Plot ist mit genau einer scale verbunden:

Implizite Definition

ggplot(
  data = gapminder_dat_trend, aes(x = time, y = co2_pcap_cons, color = country)) +
  geom_line()

Wird intern zu:

ggplot(
  data = gapminder_dat_trend, aes(x = time, y = co2_pcap_cons, color = country)) +
  geom_line() +
  scale_x_continuous() + 
  scale_y_continuous() + 
  scale_colour_discrete()

Das können wir uns zunutze machen, um manuell Scales zu definieren.

ggplot(
  data = gapminder_dat_trend, aes(x = time, y = co2_pcap_cons, color = country)) +
  geom_line() +
  scale_x_continuous(name = "Jahr") + 
  scale_y_continuous(name = "CO2 Verbrauch pro Kopf") + 
  scale_colour_discrete(name = "Länder")

In der Praxis würden wir dafür labs(x = "Jahr", y = "CO2 Verbrauch pro Kopf", color = "Länder") nutzen. Wir sehen so aber, dass Achsen- und Legendentitel jeweils Skalennamen sind.

ggplot(
  data = gapminder_dat_trend, aes(x = time, y = co2_pcap_cons, color = country)) +
  geom_line() +
  scale_x_continuous(name = "Jahr") + 
  scale_y_log10(name = "CO2 Verbrauch pro Kopf") +
  scale_colour_discrete(name = "Länder")

Eine Übersicht über die möglichen Skalentypen findet sich [hier])https://ggplot2tor.com/scales/).

Einige Anwendungsfälle

Farben

ggplot(
  data = gapminder_dat_trend, 
  aes(x = time, y = co2_pcap_cons, color = country)) +
  geom_line() +
  scale_colour_manual(values = c("deu" = "black", "usa" = "blue", "chn" = "red", "ind" = "orange", "bra" = "green", "zaf" = "purple", "rus" = "brown", "egy" = "yellow", "aus" = "pink", "mex" = "cyan", "jpn" = "grey"), name = "Länder")

Skalen-Ticks

ggplot(
  data = gapminder_dat_trend, aes(x = time, y = co2_pcap_cons, color = country)) +
  geom_line() +
  scale_x_continuous(name = "Jahr") + 
  scale_y_log10(name = "CO2 Verbrauch pro Kopf") +
  scale_colour_discrete(name = "Länder")

:::

Scale Guides

Jede Skala (und damit jede Aesthetic) bekommt einen Guide zugeordnet. Intern passiert das über guides(). Wir können guides() also nutzen, um die Legende zu manipulieren:

ggplot(
  data = gapminder_dat_trend, aes(x = time, y = co2_pcap_cons, color = country)) +
  geom_line() +
  scale_x_continuous(name = "Jahr") + 
  scale_y_log10(name = "CO2 Verbrauch pro Kopf") +
  scale_colour_discrete(name = "Länder") +
  guides(color = guide_legend(title = "Staaten", ncol = 3, reverse = TRUE, override.aes = list(linewidth = 3)))

Mögliche guide-Funktionen

::: {.column width = “50%}

ggplot(
  data = gapminder_dat_trend, aes(x = time, y = co2_pcap_cons, color = country)) +
  geom_line() +
  scale_x_continuous(name = "Jahr") + 
  scale_y_log10(name = "CO2 Verbrauch pro Kopf") +
  scale_colour_discrete(name = "Länder") +
  guides(x = guide_axis(angle = 90))

ggplot(
  data = gapminder_dat_trend, aes(x = time, y = co2_pcap_cons, color = country)) +
  geom_line() +
  scale_x_continuous(name = "Jahr", guide = guide_axis(angle = 90))

:::

Legenden mergen

Themes

Da würde man ja auch viel zur Legende ändern?

Koordinatensytem

  • Polar, evtl. characters plot oder weltraumplot als beispiel

Abspeichern

Vektor vs Raster (Rolfs 7)